package com.ruoyi.wvp.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wvp.config.MonitorPlatformConfig;
import com.ruoyi.wvp.domain.AddressUnit;
import com.ruoyi.wvp.domain.Surveillance;
import com.ruoyi.wvp.entity.AddressUnitEntity;
import com.ruoyi.wvp.sdk.monitor.IMonitorSdkService;
import com.ruoyi.wvp.sdk.monitor.entity.response.LiveHistoryInfo;
import com.ruoyi.wvp.sdk.monitor.entity.response.LiveInfo;
import com.ruoyi.wvp.service.IAddressUnitService;
import com.ruoyi.wvp.service.ISurveillanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

/**
 * @author 蒋翔 SY100242
 * @date 2024/7/23 16:41
 * @email SY100242@hnlens.com
 */
@RestController
@RequestMapping("/wvp/onlineMonitor")
@Tag(name = "【实时监控】管理")
public class OnlineMonitorController extends BaseController {

    @Autowired
    private ISurveillanceService surveillanceService;;
    @Autowired
    private IAddressUnitService addressUnitService;
    @Autowired
    private IMonitorSdkService iMonitorSdkService;
    @Autowired
    private MonitorPlatformConfig monitorPlatformConfig;

    @Operation(summary = "查看实时监控")
    @PreAuthorize("@ss.hasPermi('wvp:onlineMonitor:list')")
    @GetMapping("/list")
    public AjaxResult list() {
        Surveillance surveillance=new Surveillance();
        List<Surveillance> surveillances = surveillanceService.listAll(surveillance);
        HashSet<Long> addressUnitIds = new HashSet<>(surveillances.size());

        List<AddressUnitEntity> treeList = getAddressUnitEntities(surveillances, addressUnitIds);
        //将设备添加到树里面
        List<AddressUnit> addressUnits = addressUnitService.listAll(new AddressUnit());
        addressUnits.removeIf(res->!addressUnitIds.contains(res.getId()));
        //获取原行政单位
        addressUnits.forEach(item->{
            AddressUnitEntity addressUnitEntity = new AddressUnitEntity();
            addressUnitEntity.setId(getAddressUnitKey(item.getId()));
            addressUnitEntity.setUnitType(false);
            addressUnitEntity.setOldId(item.getId().toString());
            addressUnitEntity.setParentId(getAddressUnitKey(item.getParentId()));
            addressUnitEntity.setName(item.getName());
            addressUnitEntity.setAncestors(item.getAncestors());
            treeList.add(addressUnitEntity);
        });
        return success(treeList);
    }

    private List<AddressUnitEntity> getAddressUnitEntities(List<Surveillance> surveillances, HashSet<Long> addressUnitIds) {
        List<AddressUnitEntity> treeList = new ArrayList<>(surveillances.size());
        surveillances.forEach(item->{
            if(StringUtils.isNotEmpty(item.getAreaCode())){
                addressUnitIds.add(Long.valueOf(item.getAreaCode()));
            }
            if(StringUtils.isNotEmpty(item.getCityCode())){
                addressUnitIds.add(Long.valueOf(item.getCityCode()));
            }
            if(StringUtils.isNotEmpty(item.getStreetCode())){
                addressUnitIds.add(Long.valueOf(item.getStreetCode()));
            }
            if(StringUtils.isNotEmpty(item.getCommunityCode())){
                addressUnitIds.add(Long.valueOf(item.getCommunityCode()));
            }
            if(StringUtils.isNotEmpty(item.getResidentialCode())){
                Long residentialCode = Long.valueOf(item.getResidentialCode());
                addressUnitIds.add(residentialCode);
                AddressUnitEntity addressUnitEntity = new AddressUnitEntity();
                addressUnitEntity.setId(getSurveillanceKey(item.getId()));
                addressUnitEntity.setUnitType(true);
                addressUnitEntity.setOldId(item.getId());
                addressUnitEntity.setParentId(getAddressUnitKey(item.getResidentialCode()));
                addressUnitEntity.setName(item.getDeviceName());
                addressUnitEntity.setDeviceStatus(item.getDeviceStatus());
                addressUnitEntity.setDeviceNumber(item.getDeviceNumber());
                treeList.add(addressUnitEntity);
            }
        });
        return treeList;
    }

    private String getSurveillanceKey(String id){
        return "S"+id;
    }
    private String getAddressUnitKey(Long id){
        return "A"+id;
    }
    private String getAddressUnitKey(String id){
        return "A"+id;
    }

    @Operation(summary = "获取设备直播地址")
    @PreAuthorize("@ss.hasPermi('wvp:onlineMonitor:list')")
    @PostMapping("/live/{id}")
    public AjaxResult getLiveInfo(@PathVariable Long id){
        Surveillance surveillance = surveillanceService.getById(id);
        if(Objects.isNull(surveillance)){
            return AjaxResult.error("未找到该设备");
        }
        LiveInfo liveInfo = iMonitorSdkService.getLiveInfo(surveillance);
        return AjaxResult.success(liveInfo);
    }

    @Operation(summary = "获取设备直播地址")
    @PreAuthorize("@ss.hasPermi('wvp:onlineMonitor:list')")
    @PostMapping("/historyLive/{id}")
    public AjaxResult getHistoryLiveInfo(@PathVariable Long id,String startTime,String stopTime){
        Surveillance surveillance = surveillanceService.getById(id);
        if(Objects.isNull(surveillance)){
            return AjaxResult.error("未找到该设备");
        }
        surveillance.setStartTime(startTime);
        surveillance.setStopTime(stopTime);
        LiveHistoryInfo liveInfo = iMonitorSdkService.getLiveHistoryInfo(surveillance);
        return AjaxResult.success(liveInfo);
    }


}



